/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlclient;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Zen
 */
public class ClientSocket extends Thread{
    
    Socket client;
    boolean listen;
    boolean list;
    ClientForm form;
    boolean run;
    
    public ClientSocket(ClientForm frame, String host, int port)
    {
        form = frame;
        try
        {
            client = new Socket(host, port); //New Socket 
            form.Connected();
            listen=false;
            run=true;
        }
        catch(IOException ex)
        {
            client=null;
            form.Disconnect();
            run=false;
            JOptionPane.showMessageDialog(null, "Kết nối thất bại");
        }
    }
    
    @Override
    public void run()
    {
        while(run==true)
        {
            ObjectInputStream ois;

            Object temp=null;
            try {
                ois = new ObjectInputStream(client.getInputStream());

                try {
                    temp=(Object)ois.readObject();
                        
                    if(temp!=null&&temp.getClass()==String.class)
                    {
                        form.CleanTable();
                        if(temp.equals("EXIT"))
                        {
                            JOptionPane.showMessageDialog(null, "Mất kết nối đến server");
                            form.Disconnect();
                        }
                        else
                            JOptionPane.showMessageDialog(null, temp);
                    }
                    if(temp.getClass()==Database.Table.class)
                        form.ShowTable((Database.Table)temp);
                        
                }catch (ClassNotFoundException ex) {
                    Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
                }    
            }catch (IOException ex) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Mất kết nối đến server");
                form.Disconnect();
            }  
        }
    }
    
    public void Send(String query)
    {
        if(client!=null)
        {
            byte[] buffer =query.getBytes();
            ObjectOutputStream oos ;
        
            try {
                oos = new ObjectOutputStream(client.getOutputStream());      
                oos.writeObject(buffer); 
            
            } catch (IOException ex) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
        else
            JOptionPane.showMessageDialog(null, "Please connect to host first!");
    }
    
    public void Dispose()
    {
        try {
            client.close();
        } catch (IOException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        run=false;
    }
}
